//
//  LHRegistViewController.m
//  EPA
//
//  Created by 卢浩 on 16/4/25.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHRegistViewController.h"

@interface LHRegistViewController ()

@property (weak, nonatomic) IBOutlet UITextField *emailTF;

@property (weak, nonatomic) IBOutlet UITextField *nikeNameTF;

@property (weak, nonatomic) IBOutlet UITextField *passWordTF;

@end

@implementation LHRegistViewController

- (IBAction)registAction:(UIButton *)sender {
    if (self.emailTF.text.length > 0 && self.nikeNameTF.text.length > 0 && self.passWordTF.text.length > 0) {
        NSURL *registURl = [NSURL URLWithString:@"http://api2.pianke.me/user/reg"];
        NSMutableURLRequest *registRequest = [NSMutableURLRequest requestWithURL:registURl];
        registRequest.HTTPMethod = @"POST";
        registRequest.HTTPBody = [[NSString stringWithFormat:@"email=%@&gender=0&uname=%@&passwd=%@",self.emailTF.text,self.nikeNameTF.text,self.passWordTF.text] dataUsingEncoding:NSUTF8StringEncoding];
        NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:registRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (data) {
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
                //                NSLog(@"%@",dict);
                if (![[(NSDictionary *)dict[@"data"] allKeys] containsObject:@"msg"]) {
                    NSLog(@"个人信息:%@",dict);
                    [self showAlertControllerWithStr:@"注册成功!" IsRegist:YES];
                }else{
                    [self showAlertControllerWithStr:[NSString stringWithFormat:@"注册失败,失败原因:%@",dict[@"data"][@"msg"]] IsRegist:NO];
                }
            }
        }];
        [dataTask resume];
    }

}

- (void)showAlertControllerWithStr:(NSString *)str IsRegist:(BOOL)isRegist{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"注册" message:str preferredStyle:(UIAlertControllerStyleAlert)];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            if (isRegist) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
